<template>
  <EcDialog v-model="dialogVisible" :auto-height="true" :show-fullscreen="false" class="form-dialog" width="40%">
    <!-- 标题 -->
    <template #title>
      <div class="dialog-title">
        <SvgIcon icon="ep:upload-filled" size="18" style="margin-right: 5px;"></SvgIcon>
        <span>{{ dialogTitle }}</span>
      </div>
    </template>

    <!-- 表单数据 -->
    <el-form ref="formRef"
             v-loading="dataLoading"
             :model="formData"
             :rules="formRules"
             label-width="100px"
             status-icon>
      <el-form-item label="快递名称：" prop="shipping_name">
        <el-input v-model="formData.shipping_name" placeholder="请输入快递名称"/>
      </el-form-item>

      <el-form-item label="快递单号：" prop="shipping_code">
        <el-input v-model="formData.shipping_code" placeholder="请输入快递单号"/>
      </el-form-item>

      <el-form-item label="发货备注：" prop="shipping_memo">
        <el-input v-model="formData.shipping_memo" placeholder="请输入发货备注"/>
      </el-form-item>

      <el-form-item label="快递查询：">
        <el-link href="https://www.kuaidi100.com/" style="font-size: 13px" target="_blank">点击跳转网页查询</el-link>
      </el-form-item>
    </el-form>

    <!-- 底部操作区域 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button :loading="dataLoading" @click="dialogVisible = false">取 消</el-button>
        <el-button v-permission="['shop:order:delivery']" :loading="dataLoading" type="primary" @click="onSubmitForm()">确 定</el-button>
      </span>
    </template>
  </EcDialog>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import { ElLoading, ElNotification, FormInstance, FormRules } from 'element-plus';
import { EcDialog } from '@/components/dialog';
import { SvgIcon } from '@/components/icon';
import useDialog from '@/hooks/use-dialog.ts';
import { orderDelivery } from '@/api/api-shop.ts';

defineOptions({name: 'order-delivery-form'})

const formRef = ref<FormInstance>();
const formLoading = ref(false); // 表单是否加载中
const {dialogVisible, dialogTitle, dialogMode, dialogModeMap, dialogIconMap} = useDialog(false);

export type OrderDelivery = {
  id: number;
  order_no: string;
  shipping_name?: string;
  shipping_code?: string;
  shipping_memo?: string;
};
// 默认表单数据
const defaultForm: OrderDelivery = <OrderDelivery>{};
// 表单数据
const formData = ref({...defaultForm});

// 表单验证规则
const formRules = reactive<FormRules>({
  shipping_name: [{required: true, message: '快递名称不能为空', trigger: 'blur'}],
  shipping_code: [{required: true, message: '快递单号不能为空', trigger: 'blur'}],
});

/** 判断数据是否处于加载中 */
const dataLoading = computed(() => {
  return formLoading.value;
})

/** 打开弹窗 */
const open = async (data: any) => {
  dialogTitle.value = '订单发货';
  dialogVisible.value = true;
  // 重置表单
  handleFormReset();

  // 设置表单值
  formData.value = <OrderDelivery>{
    id: data.id,
    shipping_code: data.shipping_code,
    shipping_name: data.shipping_name,
    shipping_memo: data.shipping_memo,
  }
}
// 提供 open 方法，用于打开弹窗
defineExpose({open});

/** 表单提交 */
const emit = defineEmits(['refresh']);
const onSubmitForm = async () => {
  await formRef.value?.validate((valid, fields) => {
    if (!valid) {
      return
    }
    const loading = ElLoading.service({
      lock: true,
      text: '正在提交中...',
      background: 'rgba(0, 0, 0, 0.2)'
    })
    // 提交数据
    orderDelivery(formData.value).then(response => {
      // 提示信息
      ElNotification({title: '成功', message: '操作完成', type: 'success', duration: 2000})
      // 发送操作成功的事件
      emit('refresh')
      // 隐藏窗口
      dialogVisible.value = false
    }).finally(() => loading.close())

  });
};

/** 重置表单 */
const handleFormReset = () => {
  // 重置表单数据
  formData.value = {...defaultForm};
  // 重置界面表单数据
  formRef.value?.resetFields();
};

</script>

<style lang="scss" scoped>
.form-dialog {
  .el-select,
  .el-input-number {
    max-width: 13rem;
  }
}
</style>
